package com.feicheng.house.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.house.entity.Card;
import com.feicheng.house.entity.Vip;
import com.feicheng.house.mapper.VipMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.ICardService;
import com.feicheng.house.service.IVipService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip> implements IVipService {
	
	@Autowired
	ICardService cardService;
	
	@Override
	public ResultInfo addVip(Vip vip) {
		//根据cardId找出card对象
		Card card = cardService.getById(vip.getCardId());
		//用会员卡有效分钟初始化会员剩余时间（分钟）
		String timeLeft = String.valueOf(24*60*card.getPeriodDate());
		vip.setTimeLeft(timeLeft);
		//初始化会员注册时间
		vip.setRegDate(LocalDateTime.now());
		//会员到期时间 = 会员注册时间 + 会员卡有效分钟
		vip.setVipDeadline(vip.getRegDate().plusMinutes(24*60*card.getPeriodDate()));
		return ResultInfo.success(this.save(vip));
	}
	
	@Override
	public ResultInfo deleteVip(Integer vipId) {
		Vip vip = this.getById(vipId);
		vip.setDeleteFlag(false);
		return ResultInfo.success(this.updateById(vip));
	}
	
	@Override
	public ResultInfo updateVip(Vip vip) {
		return ResultInfo.success(this.updateById(vip));
	}
	
//	@Override
//	public ResultInfo getOneVip(Integer vipId) {
//		return ResultInfo.success(this.getById(vipId));
//	}

}
